﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightChariotFactory : NetUnionFightBase
    {
        public Dictionary<long, InstUFCariotFactory> chariotFartorys = new Dictionary<long, InstUFCariotFactory>();
        public Dictionary<long, float> recoverTime = new Dictionary<long, float>();
        public NetUnionFightChariotFactory(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {

        }

        public override void Enter()
        {
            base.Enter();
            chariotFartorys.Clear();
        }

        public override void Leave()
        {
            base.Leave();
            chariotFartorys.Clear();
        }

        public override RoomCharactor CreateCharactor(long uuid)
        {
            var inst = chariotFartorys[uuid];
            charactorManager.CreateUnionChariotFartory(inst);
            chariotFartorys[uuid] = inst;
            return charactorManager.GetCharactor(uuid+"");
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (chariotFartorys.ContainsKey(uuid))
                chariotFartorys.Remove(uuid);
        }

        /// <summary>
        /// 刷新工厂
        /// </summary>
        /// <param name="factory"></param>
        public void ChariotFactoryRefresh(InstUFCariotFactory factory)
        {
            var uuid = factory.guid;
            chariotFartorys[uuid] = factory;
            states[uuid] = factory.state;

            var charactor = GetCharactor(uuid,false);
            if (charactor == null)
            {
                charactor = CreateCharactor(uuid);
                postions[uuid] = charactor.transform.position;
            }

            Debug.Log("uuid=" + uuid + " " + "[FACTORY state]={" + (UnionChariotFactoryState)factory.state + "}");
            UpdateState(uuid, factory.state);

            
            //刷新守卫
            if (factory.guard != null)
            {
                net.netGuard.GuardRefresh(factory.guard);
            }
            else
            {
                Debug.Log("[Chariot Factory is null]");
            }
        }

        /// <summary>
        /// 战车工厂工作
        /// </summary>
        /// <param name="msg"></param>
        public void ChariotFactoryWorking(SCUFCariotFactoryBuilding msg)
        {
            var uuid =  msg.factoryGuid;
            
            UpdateState(uuid, (int)UnionChariotFactoryState.Actived);
        }
        /// <summary>
        /// 战车工厂造车完毕
        /// </summary>
        /// <param name="msg"></param>
        public void ChariotFactoryBuildComplete(SCUFCariotFactoryBuildComplete msg)
        {
            var uuid = msg.factoryGuid;

            var chariotId = msg.cariot.guid;
            net.netChariot.ChariotRefresh(msg.cariot);
            UpdatePosition(chariotId, new Vector3(msg.pos.x, msg.pos.y, msg.pos.z));
            if (!angles.ContainsKey(uuid))
                angles[uuid] = 0f;
            var chariot = GetCharactor(chariotId);
            var move = chariot.move as RCMoveChariot;
            move.PlaceTo(postions[uuid], angles[uuid]);
            chariotFartorys[uuid].cariotGuid = chariotId;

            UpdateState(uuid, (int)UnionChariotFactoryState.PrepareComplete);
        }

        /// <summary>
        /// 战车工厂开启（野外的）
        /// </summary>
        /// <param name="msg"></param>
        public void FieldChariotFactoryOpen(SCUFCariotFactoryOpen msg)
        {
            ChariotFactoryRefresh(msg.factory);

            var guard = msg.factory.guard;

            if(guard!=null)
            {
                net.netGuard.GuardRefresh(guard);
            }
            else
            {
                Debug.Log("[ERROR] Guard inst is null");
            }
        }
        /// <summary>
        /// 战车工厂关闭（野外的）
        /// </summary>
        /// <param name="msg"></param>
        public void FieldChariotFactoryClose(SCUFCariotFactoryClose msg)
        {
            var uuid =  msg.factoryGuid;
            
            chariotFartorys[uuid].state = (int)UnionChariotFactoryState.Unactived;
            UpdateState(uuid, (int)UnionChariotFactoryState.Unactived);

            recoverTime[uuid] = (float)msg.recoverTime;
        }
    }
}